package com.tsx.china.mobile.ai.embedding;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.DocumentTransformer;
import org.springframework.ai.reader.JsonReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class RuleEmbedding {
    private final VectorStore vectorStore;

    @PostConstruct
    public void init() {
        Resource resource = new ClassPathResource("rule-data.json");
        JsonReader jsonReader = new JsonReader(resource,"type","condition","description");
        List<Document> documentList = jsonReader.get();
        DocumentTransformer documentTransformer = new TokenTextSplitter(false);
        List<Document> apply = documentTransformer.apply(documentList);
        vectorStore.add(apply);
        log.info("向量库初始化完成 :{}", apply.size());
    }
}
